import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/text/gt_text.dart';

//tag高度
enum GTTagSizeV3 { size16, size20 }

enum GTTagTypeV3 {
  blue,
  red,
  green,
  yellow,
  purplish_red,
  gray,
  new_type,
  hot,
  red_gradient,
  blue_gradient,
  red_up,
  red_down,
  green_up,
  green_down,
  white,
  orange,
  new_small
}

class GTTagV3 extends StatelessWidget {
  final String? text;
  final GTTagSizeV3? tagSize;
  final GTTagTypeV3? tagType;
  final IconData? startIcon;
  final IconData? endIcon;
  final bool? isEnabled;
  final Color? iconColor;
  final VoidCallback? onPressed;
  final double? topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius;
  final bool? showClose;
  final VoidCallback? closeClick;
  final bool?showInfoIcon;
  const GTTagV3(
      {super.key,
      this.text,
      this.startIcon,
      this.endIcon,
      this.isEnabled = true,
      this.onPressed,
      this.tagSize = GTTagSizeV3.size20,
      this.tagType = GTTagTypeV3.blue,
      this.topLeftRadius,
      this.topRightRadius,
      this.bottomLeftRadius,
      this.bottomRightRadius,
      this.iconColor,
      this.showClose = false,
      this.closeClick,
      this.showInfoIcon});

  @override
  Widget build(BuildContext context) {
    IconData? _startIcon = startIcon;
    String _text = text ?? "";
    bool _isEnabled = isEnabled ?? true;
    Color startColor = GTColorsV3.gradient7Start.color, endColor = GTColorsV3.gradient7End.color;
    double tagHeight = 0;
    double iconSize = 12;
    double textSize;
    bool fixedWidth = false;
    bool showGradient = false;
    late double lt, lb, rt, rb;
    switch (tagSize ?? GTTagSizeV3.size20) {
      case GTTagSizeV3.size16:
        tagHeight = 16;
        textSize = 10;
        lt = 2;
        lb = 2;
        rt = 2;
        rb = 2;
        break;
      case GTTagSizeV3.size20:
        tagHeight = 20;
        textSize = 12;
        lt = 4;
        lb = 4;
        rt = 4;
        rb = 4;
        break;
    }

    TextStyle textStyle = gtTextStyleW400();
    /*
    Tag不同类型样式设置
     */
    Color backgroundColor;
    Color textColor;
    double padding = 4;
    switch (tagType ?? GTTagTypeV3.blue) {
      case GTTagTypeV3.blue:
        backgroundColor = GTColorsV3.cmpt15.color;
        textColor = GTColorsV3.text5.color;
        break;
      case GTTagTypeV3.red:
        backgroundColor = GTColorsV3.cmpt4.color;
        textColor = GTColorsV3.pd1.color;
        break;
      case GTTagTypeV3.green:
        backgroundColor = GTColorsV3.cmpt12.color;
        textColor = GTColorsV3.pd2.color;
        break;
      case GTTagTypeV3.yellow:
        backgroundColor = GTColorsV3.cmpt3.color;
        textColor = GTColorsV3.funct4.color;
        break;
      case GTTagTypeV3.purplish_red:
        backgroundColor = GTColorsV3.cmpt14.color;
        textColor = GTColorsV3.funct8.color;
        break;
      case GTTagTypeV3.gray:
        backgroundColor = GTColorsV3.cmpt11.color;
        textColor = GTColorsV3.text2.color;
        break;
      case GTTagTypeV3.new_type:
        backgroundColor = GTColorsV3.funct9.color;
        textColor = GTColorsV3.text6.color;
        textStyle = gtTextStyleW500();
        padding = 6;
        break;
      case GTTagTypeV3.hot:
        backgroundColor = GTColorsV3.funct10.color;
        textColor = GTColorsV3.text6.color;
        textStyle = gtTextStyleW500();
        padding = 6;
        break;
      case GTTagTypeV3.red_gradient:
        startColor = GTColorsV3.gradient6Start.color;
        endColor = GTColorsV3.gradient6End.color;
        backgroundColor = Colors.transparent;
        textColor = GTColorsV3.text6.color;
        textStyle = gtTextStyleW500();
        padding = 6;
        showGradient = true;
        break;
      case GTTagTypeV3.blue_gradient:
        startColor = GTColorsV3.gradient5Start.color;
        endColor = GTColorsV3.gradient5End.color;
        backgroundColor = Colors.transparent;
        textColor = GTColorsV3.text6.color;
        textStyle = gtTextStyleW500();
        padding = 6;
        showGradient = true;
        break;
      case GTTagTypeV3.red_up:
        tagHeight = 16;
        iconSize = 10;
        fixedWidth = true;
        _startIcon = GTIconFont.e7e1;
        backgroundColor = GTColorsV3.pd1.color;
        textColor = GTColorsV3.icon6.color;
        _text = "";
        break;
      case GTTagTypeV3.red_down:
        tagHeight = 16;
        iconSize = 10;
        fixedWidth = true;
        _startIcon = GTIconFont.e7e3;
        backgroundColor = GTColorsV3.pd1.color;
        textColor = GTColorsV3.icon6.color;
        _text = "";
        break;
      case GTTagTypeV3.green_up:
        tagHeight = 16;
        iconSize = 10;
        fixedWidth = true;
        _startIcon = GTIconFont.e7e1;
        backgroundColor = GTColorsV3.pd2.color;
        textColor = GTColorsV3.icon6.color;
        _text = "";
        break;
      case GTTagTypeV3.green_down:
        tagHeight = 16;
        iconSize = 10;
        fixedWidth = true;
        _startIcon = GTIconFont.e7e3;
        backgroundColor = GTColorsV3.pd2.color;
        textColor = GTColorsV3.icon6.color;
        _text = "";
        break;
      case GTTagTypeV3.white:
        backgroundColor = GTColorsV3.icon6.color;
        textColor = GTColorsV3.cmpt6.color;
        break;
      case GTTagTypeV3.orange:
        backgroundColor = GTColorsV3.cmpt13.color;
        textColor = GTColorsV3.funct10.color;
        break;
      case GTTagTypeV3.new_small:
        backgroundColor = GTColorsV3.funct9.color;
        textColor = GTColorsV3.text6.color;
        tagHeight = 16;
        textStyle = gtTextStyleW700();
        lt = 2;
        lb = 6;
        rt = 6;
        rb = 6;
        break;
    }
    Color _iconColor = iconColor ?? textColor;

    return GestureDetector(
      onTap: () {
        if (_isEnabled) {
          onPressed?.call();
        }
      },
      child: IntrinsicWidth(
        child: Container(
          width: fixedWidth ? 16 : null,
          height: tagHeight,
          padding: EdgeInsets.symmetric(horizontal: fixedWidth ? 0 : padding),
          decoration: showGradient
              ? BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(topLeftRadius ?? lt),
                    topRight: Radius.circular(topRightRadius ?? lb),
                    bottomLeft: Radius.circular(bottomLeftRadius ?? rt),
                    bottomRight: Radius.circular(bottomRightRadius ?? rb),
                  ),
                  gradient: LinearGradient(
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                    colors: [startColor, endColor],
                    // 添加 stops 可以设置每个颜色的位置（范围是0到1）
                    stops: const [0.0, 1.0],
                  ),
                )
              : BoxDecoration(
                  color: _isEnabled ? backgroundColor : GTColorsV3.cmpt11.color,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(topLeftRadius ?? lt),
                    topRight: Radius.circular(topRightRadius ?? lb),
                    bottomLeft: Radius.circular(bottomLeftRadius ?? rt),
                    bottomRight: Radius.circular(bottomRightRadius ?? rb),
                  ),
                ),
          child: Align(
            alignment: fixedWidth ? Alignment.center : Alignment.centerLeft,
            child: Row(
              mainAxisSize: fixedWidth ? MainAxisSize.min : MainAxisSize.max,
              children: [
                if (_startIcon != null) ...[
                  Icon(_startIcon, size: iconSize, color: _iconColor),
                  if (!fixedWidth) const SizedBox(width: 4),
                ],
                if (!fixedWidth) ...[
                  Expanded(
                    child: GTText(
                      removeTopBottomPadding: true,
                      _text,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: textStyle.copyWith(
                          color: _isEnabled ? textColor : GTColorsV3.text7.color, fontSize: textSize),
                    ),
                  ),
                ],
                if (endIcon != null) ...[
                  const SizedBox(width: 4),
                  Icon(
                    endIcon,
                    size: iconSize,
                    color: _iconColor,
                  ),
                ],
                if (showClose ?? false) ...[
                  GestureDetector(
                    onTap: closeClick,
                    child: Padding(
                      padding: GTEdgeInsetsOnly(left: 2),
                      child: Icon(
                        GTIconFont.e89b,
                        size: 12,
                        color: GTColorsV3.icon3.color,
                      ),
                    ),
                  ),
                ],
                if (showInfoIcon??false) ...[
                  const SizedBox(width: 2),
                  Icon(
                    GTIconFont.ea01,
                    size: 10,
                    color: _iconColor,
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}
